const express = require('express')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
const passport = require('passport')

// 引入路由列表
const user = require('./routers/api/user')
const profile = require('./routers/api/profile')

const {
  port,
  mongodbURI
} = require('./config/keys')

const app = express()

// use body-parse
app.use(bodyParser.urlencoded({
  extended: false
}))
app.use(bodyParser.json())

// passport init
app.use(passport.initialize())
require('./config/passport')(passport)

// 使用router
app.use('/api/user', user)
app.use('/api/profile', profile)

// connect to mongodb
mongoose.connect(mongodbURI, {
    useNewUrlParser: true
  })
  .then(() => {
    console.log('mongodb connected')
  })
  .catch((err) => {
    console.log(err)
  })



// 监听端口号
app.listen(3000, () => {
  console.log(`server is run port in ${port}`)
})